package com.qu.waibao.my.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qu.waibao.R;
import com.qu.waibao.base.BaseRecyclerAdapter;
import com.qu.waibao.bean.Order;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @描述 describe
 * @作者 Administrator
 * @创建时间 2018/7/24 9:56
 * @邮箱 1123599561@qq.com
 */
public class PublishAdapter extends BaseRecyclerAdapter<Order> {
    public PublishAdapter(Context context, int mode) {
        super(context, mode);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) {
        return new PublishViewHolder(mInflater.inflate(R.layout.publish_recycler_item,parent,false));
    }

    @Override
    protected void onBindDefaultViewHolder(RecyclerView.ViewHolder holder, Order item, int position) {
        PublishViewHolder publishViewHolder = (PublishViewHolder) holder;
        String status = item.getState();
        publishViewHolder.tv_ordercode.setText(item.getOrdercode());
        publishViewHolder.tv_status.setText(status);
        publishViewHolder.tv_remark.setText(item.getRemark());
        if(item.getMoney_total().equals("0.00")){
            publishViewHolder.tv_money.setVisibility(View.GONE);
        }else {
            publishViewHolder.tv_money.setText("¥"+item.getMoney_total());
        }
        publishViewHolder.tv_type.setText(item.getServicecategoryname());
        publishViewHolder.tv_time.setText(item.getCreatedate());
        if(status.equals("待抢单")){
            publishViewHolder.tv_status.setTextColor(Color.parseColor("#3a9de2"));
            publishViewHolder.rl_bottom.setVisibility(View.VISIBLE);
            publishViewHolder.tv_cancel.setVisibility(View.VISIBLE);
            publishViewHolder.tv_payment.setVisibility(View.GONE);
            publishViewHolder.tv_zhui_money.setVisibility(View.GONE);
            publishViewHolder.tv_finished.setVisibility(View.GONE);
            publishViewHolder.tv_comment.setVisibility(View.GONE);
        }else if(status.equals("待完成")){
            publishViewHolder.tv_status.setTextColor(Color.parseColor("#cc3333"));
            publishViewHolder.rl_bottom.setVisibility(View.VISIBLE);
            publishViewHolder.tv_cancel.setVisibility(View.GONE);
            publishViewHolder.tv_payment.setVisibility(View.GONE);
            publishViewHolder.tv_zhui_money.setVisibility(View.VISIBLE);
            publishViewHolder.tv_finished.setVisibility(View.VISIBLE);
            publishViewHolder.tv_comment.setVisibility(View.GONE);
        }else if(status.equals("待支付")){
            publishViewHolder.tv_status.setTextColor(Color.parseColor("#f29f1b"));
            publishViewHolder.rl_bottom.setVisibility(View.VISIBLE);
            publishViewHolder.tv_cancel.setVisibility(View.VISIBLE);
            publishViewHolder.tv_payment.setVisibility(View.VISIBLE);
            publishViewHolder.tv_zhui_money.setVisibility(View.GONE);
            publishViewHolder.tv_finished.setVisibility(View.GONE);
            publishViewHolder.tv_comment.setVisibility(View.GONE);
        }else if(status.equals("已完成")){
            publishViewHolder.tv_status.setTextColor(Color.parseColor("#333333"));
            publishViewHolder.tv_cancel.setVisibility(View.GONE);
            publishViewHolder.tv_payment.setVisibility(View.GONE);
            publishViewHolder.tv_zhui_money.setVisibility(View.GONE);
            publishViewHolder.tv_finished.setVisibility(View.GONE);
            if(item.getIfcommented_user().equals("1")){
                publishViewHolder.rl_bottom.setVisibility(View.GONE);
            }else {
                publishViewHolder.tv_comment.setVisibility(View.VISIBLE);
                publishViewHolder.rl_bottom.setVisibility(View.VISIBLE);
            }
        }else if(status.equals("已取消")){
            publishViewHolder.tv_status.setTextColor(Color.parseColor("#333333"));
            publishViewHolder.rl_bottom.setVisibility(View.GONE);
        }

        publishViewHolder.tv_cancel.setTag(position);
        publishViewHolder.tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                if(onItemButtonClickListener != null){
                    onItemButtonClickListener.onItemButtonClick(position,0);
                }
            }
        });

        publishViewHolder.tv_payment.setTag(position);
        publishViewHolder.tv_payment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                if(onItemButtonClickListener != null){
                    onItemButtonClickListener.onItemButtonClick(position,1);
                }
            }
        });

        publishViewHolder.tv_zhui_money.setTag(position);
        publishViewHolder.tv_zhui_money.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                if(onItemButtonClickListener != null){
                    onItemButtonClickListener.onItemButtonClick(position,2);
                }
            }
        });

        publishViewHolder.tv_finished.setTag(position);
        publishViewHolder.tv_finished.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                if(onItemButtonClickListener != null){
                    onItemButtonClickListener.onItemButtonClick(position,3);
                }
            }
        });

        publishViewHolder.tv_comment.setTag(position);
        publishViewHolder.tv_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                if(onItemButtonClickListener != null){
                    onItemButtonClickListener.onItemButtonClick(position,4);
                }
            }
        });

    }

    class PublishViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.tv_ordercode)
        TextView tv_ordercode;
        @BindView(R.id.tv_status)
        TextView tv_status;
        @BindView(R.id.tv_remark)
        TextView tv_remark;
        @BindView(R.id.tv_money)
        TextView tv_money;
        @BindView(R.id.tv_type)
        TextView tv_type;
        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.tv_cancel)
        TextView tv_cancel;
        @BindView(R.id.tv_payment)
        TextView tv_payment;
        @BindView(R.id.tv_zhui_money)
        TextView tv_zhui_money;
        @BindView(R.id.tv_finished)
        TextView tv_finished;
        @BindView(R.id.tv_comment)
        TextView tv_comment;
        @BindView(R.id.rl_bottom)
        RelativeLayout rl_bottom;
        public PublishViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    private OnItemButtonClickListener onItemButtonClickListener;
    public interface OnItemButtonClickListener{
        void onItemButtonClick(int position,int flag);
    }
    public void setOnItemCancleClickListener(OnItemButtonClickListener onItemButtonClickListener){
        this.onItemButtonClickListener = onItemButtonClickListener;
    }

}
